package com.heima.behavior.service.impl;

import com.heima.behavior.pojo.ApCollectionBehavior;
import com.heima.behavior.pojo.ApUnlikesBehavior;
import com.heima.behavior.service.CollectionBehaviorService;
import com.heima.model.behavior.dto.CollectionBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.threadUtil.ThreadLocalUtil;
import com.heima.utils.common.threadUtil.UserInfo;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class CollectionBehaviorServiceImpl implements CollectionBehaviorService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private MongoTemplate mongoTemplate;
    @Override
    public ResponseResult collection(CollectionBehaviorDto dto) {
        //参数校验
        if(dto==null||dto.getEntryId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //线程中拿到用户id
        UserInfo userInfo = ThreadLocalUtil.get();
        if(userInfo==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        String user_collection_key="USER_COLLECTION_"+dto.getEntryId();
        //判断用户是否点过收藏
        Boolean flag = stringRedisTemplate.opsForSet().isMember(user_collection_key, userInfo.getId().toString());
//        if(flag&&dto.getType()==0){
//            //用户点过一次收藏,又传了一次收藏参数
//            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
//        }
        //点过收藏，取消收藏操作
        if(flag&&dto.getType()==1){
            //缓存表和数据库
            //删除redis缓存表
            stringRedisTemplate.opsForSet().remove(user_collection_key,userInfo.getId().toString());

            //构造数据库查询条件
            Query query = Query.query(Criteria.where("userId").is(userInfo.getId()).and("articleId").is(dto.getEntryId()));
         //删除数据
            mongoTemplate.remove(query, ApCollectionBehavior.class);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        //未点过收藏,点击收藏操作
        if(!flag&&dto.getType()==0){
            //添加到缓存表
            stringRedisTemplate.opsForSet().add(user_collection_key,userInfo.getId().toString());
            //添加数据到mongoDB
            //封装ApCollectionBehavior
            ApCollectionBehavior apCollectionBehavior = new ApCollectionBehavior();
            apCollectionBehavior.setCollectionTime(new Date());
            apCollectionBehavior.setArticleId(dto.getEntryId());
            apCollectionBehavior.setUserId(userInfo.getId().longValue());
            apCollectionBehavior.setType(dto.getType());

            mongoTemplate.insert(apCollectionBehavior);
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);


    }
}
